package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Attendance;
import com.example.demo.entity.dto.PageQuery;
import com.example.demo.mapper.AttendanceMapper;
import com.example.demo.service.AttendanceService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AttendanceServiceimpl extends ServiceImpl<AttendanceMapper, Attendance> implements AttendanceService {

    @Override
    public List<Attendance> getAllAttendance() {
        return baseMapper.selectList(null);
    }

    @Override
    public List<Attendance> getAttendanceByNumber(int number) {
        QueryWrapper<Attendance> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("number", number);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public boolean addAttendance(Attendance attendance) {
        // 检查考勤信息是否已存在
        QueryWrapper<Attendance> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("number", attendance.getNumber());
        Attendance existingAttendance = baseMapper.selectOne(queryWrapper);
        if (existingAttendance != null) {
            return false; // 假条已存在，不插入
        }
        return save(attendance);
    }

    @Override
    public boolean updateAttendance(Attendance attendance) {
        return updateById(attendance);
    }
    @Override
    public IPage<Attendance> page(PageQuery pageQuery) {
        Page<Attendance> pageInfo = new Page<>(pageQuery.getPage(), pageQuery.getSize());
        QueryWrapper<Attendance> wrapper = new QueryWrapper<>();
        Integer attId = pageQuery.getAttId();
        if(attId != null) {
            // 可以添加查询条件
            wrapper.eq("number", pageQuery.getAttId());
        }
        return baseMapper.selectPage(pageInfo, wrapper);
    }
}